<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
  <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
  <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
  <style>
    .layui-upload-img{
      width: 100px;
    }
    .layui-table th{
      padding: 9px 6px;
    }
  </style>
</head>
<body>
<div style="margin-top: 20px;">
</div>

<form class="layui-form" id="admin">
  <input type="hidden" name="id" value="{$data.mk_id}">
  <div class="layui-form-item " id="">
    <label class="layui-form-label">营销名称</label>
    <div class="layui-input-inline" style="width: 500px">
      <input name="mk_name" value="{$data.mk_name}" placeholder="请输入优惠券名称" autocomplete="off" class="layui-input"  type="text">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">活动状态</label>
    <div class="layui-input-inline" >
      <input type="radio"  lay-filter="goodstype" name="status" value="1"  {if($data.status == 1)}checked{/if} title="启用" >
      <input type="radio"  lay-filter="goodstype" name="status" value="2" {if($data.status == 2)}checked{/if} title="停用" >
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">活动时间限制</label>
    <div class="layui-input-inline" style="width: 280px">
      <input type="radio"  lay-filter="time_chose" name="time_chose" value="1" {if($data.time_chose ==1 )}checked{/if} title="不限制" >
      <input type="radio"  lay-filter="time_chose" name="time_chose" value="2"  {if($data.time_chose ==2 )}checked{/if} title="固定起止时间" >
    </div>
  </div>
  <div class="layui-form-item" id="time_chose"  {if($data.time_chose ==1 )}hidden{/if}>
  <label class="layui-form-label">活动时间</label>
  <div class="layui-input-inline" style="width: 500px">
    <div class=" layui-col-xs5">
      <input name="starttime" value="{notempty name="$data.starttime"}{$data.starttime||date='Y-m-d H:i:s',###}{/notempty}" id="starttime" lay-verify="required" placeholder="请输入开始时间" autocomplete="off" class="layui-input" type="text">
    </div>
    <div class="layui-col-xs2" style="text-align: center; line-height: 38px">~</div>
    <div class=" layui-col-xs5">
      <input name="endtime" value="{notempty name="$data.endtime"}{$data.endtime||date='Y-m-d H:i:s',###}{/notempty}" id="endtime" lay-verify="required" placeholder="请输入结束时间" autocomplete="off" class="layui-input layui-col-xs5" type="text">
    </div> </div>
  </div>

  {if($data['mk_id'] == 2 )}
  <div class="layui-form-item">
    <label class="layui-form-label">领券规则</label>
    <div class="layui-input-inline" style="width: 330px;">
      <input type="radio"  lay-filter="send_type" name="send_type" value="1"  {if($data.send_type == 1)}checked{/if} title="分享可领" >
      <input type="radio"  lay-filter="send_type" name="send_type" value="2" {if($data.send_type == 2)}checked{/if} title="分享注册新人数量" >
    </div>
  </div>
  <div class="layui-form-item " id="share" {if($data.send_type == 1)}hidden{/if}>
  <label class="layui-form-label">邀请注册量</label>
  <div class="layui-input-inline" style="width: 500px">
    <input name="share"  value="{$data.share}" placeholder="请输入领券优惠券需要的新人注册数量" autocomplete="off" class="layui-input"  type="text">
  </div>
  </div>
  {else/}
  <input type="hidden" name="send_type" value="1" >
  <input type="hidden" name="share" value="0" >
  {/if}

  <!--品类-->
  <div class="layui-form-item" id="model_type" >
    <div class="layui-form-item " >
      <label class="layui-form-label">优惠券</label>
      <div class="layui-input-inline" style="position: relative;z-index: 10000;width: 400px">
        <select  id="trip_class"  lay-filter="addtrips_class"   lay-verify="required" lay-search>
          {volist name="coupon" id="vo"}
          <option value="{$vo.coupon_id}">{$vo.coupon_name}</option>
          {/volist}
        </select>
      </div>
      <span id="addtrips_class" data-name="全部" data-id="{$coupon[0]['coupon_id']}" class="layui-btn layui-btn-danger layui-btn-sm" style="margin-top: 4px">添加</span>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">已选优惠券</label>
      <div class="layui-input-inline" style="width: 500px" id="tripnames_class">
        {volist name='list' id='vo'}
        <span class="layui-btn layui-btn-sm layui-btn-normal deltripspan_class" data-id="{$vo.coupon_id}" style="margin: 5px 10px">{$vo.coupon_name}<i class="layui-icon"></i></span>
        {/volist}
      </div>
    </div>
    <input name="coupon_ids" id="tripids_class"  value="{$data.coupon_ids}"   type="hidden">
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>

<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>

<script>
    layui.use(['layer', 'form','laydate'], function() {
        // var laydate = layui.laydate;
        var layer = layui.layer,
            $ = layui.jquery,
            laydate= layui.laydate,
            form = layui.form;
        laydate.render({
            'type':'datetime',
            elem: '#starttime' //指定元素
            ,trigger:'click'
        });
        laydate.render({
            'type':'datetime',
            elem: '#endtime' //指定元素
            ,trigger:'click'
        });

        $(window).on('load', function() {
            //品类选择
            form.on('select(addtrips_class)', function(data){
                var tripid=data.value;
                $("#addtrips_class").attr("data-id",tripid);
            });
            $("#addtrips_class").on("click",function () {
                var tripid= $("#addtrips_class").attr("data-id");
                var tripids=$("#tripids_class").val();
                var ids=tripids.split(",");
                if (tripid==999999){
                    tripids=999999;
                    var tripspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan_class" data-id="999999" style="margin: 5px 10px">暂不设置<i class="layui-icon"></i></span>';
                    $("#tripnames_class").html(tripspan);
                }else{
                    for(var i=0 ; i < ids.length; i++){
                        if(tripid==ids[i]){
                            layer.msg("不能重复添加商品");
                            return false;
                        }
                        if(ids[i]==999999){
                            tripids=999999;
                            var tripspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan_class" data-id="999999" style="margin: 5px 10px">暂不设置<i class="layui-icon"></i></span>';
                            $("#tripnames_class").html(tripspan);
                            $("#tripids_class").val(tripids);
                            return false;
                        }
                    }
                    var tripname="";
                    $("#trip_class").find("option").each(function () {
                        if($(this).val()==tripid){
                            tripname=$(this).html();
                        }
                    });
                    var tripspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan_class" data-id="'+tripid+'" style="margin: 5px 10px">'+tripname+'<i class="layui-icon"></i></span>';
                    if(!tripids){
                        tripids+=tripid;
                    }else{
                        tripids+=','+tripid;
                    }
                    $("#tripnames_class").append(tripspan);
                }
                $("#tripids_class").val(tripids);
            });
            $(document).on("click",'.deltripspan_class',function () {
                var dataid=$(this).attr("data-id");
                var tripids=$("#tripids_class").val();
                var ids=tripids.split(",");
                var newid="";
                for(var i=0 ; i < ids.length; i++){
                    if(ids[i]&&dataid!=ids[i]){
                        if(!newid){
                            newid+=ids[i];
                        }else{
                            newid+=","+ids[i];
                        }
                    }
                }
                $("#tripids_class").val(newid);
                $(this).remove();
            });
            form.on('radio(send_type)', function(data){
                var val= $(this).val();
                if (val=='1'){
                    $('#share').hide();
                }else  if (val=='2') {
                    $('#share').show();
                }
            });
            form.on('radio(time_chose)', function(data){
                var val= $(this).val();
                if (val=='1'){
                    $('#time_chose').hide();
                }else  if (val=='2') {
                    $('#time_chose').show();
                }
            });
            form.on('submit(admin)', function(data) {
                $.ajax({
                    url:"{:url('admin/Coupon/upmarket')}",
                    data:$('#admin').serialize(),
                    type:'post',
                    async: false,
                    success:function(res) {
                        layer.msg(res.msg);
                        if(res.code == 1) {
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            },1500)
                        }
                    }
                });
                return false;
            });
        });
    });
</script>
</body>
</html>
